package org.dingyom.product.service.impl;

import org.dingyom.org.domain.Department;
import org.dingyom.product.domain.ProductType;
import org.dingyom.product.mapper.ProductTypeMapper;
import org.dingyom.product.service.IProductTypeService;
import org.dingyom.basics.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author dingyom
 * @since 2022-08-25
 */
@Service
public class ProductTypeServiceImpl extends BaseServiceImpl<ProductType> implements IProductTypeService {
    @Autowired
    ProductTypeMapper mapper;
    @Override
    public List<ProductType> getTree() {
        return mapper.loadTree();
    }
    @Override
    public List<ProductType> getParent() {
        return mapper.loadParent();
    }

    @Override
    public void modify(ProductType productType) {
        //获取path
        Long deptId = productType.getId();
        String path = null;
        if(productType.getParent()==null||productType.getParent().getId()==null){//判断是否是顶级部门
            path = "/"+deptId;
        }else {
            Long pid = productType.getParent().getId();
            path = super.getOne(pid).getPath() + "/" +deptId;
        }
        productType.setPath(path);
        super.modify(productType);
    }

    @Override
    public void add(ProductType productType) {
        super.add(productType);
        //获取path
        Long deptId = productType.getId();
        String path = null;
        if(productType.getParent()==null||productType.getParent().getId()==null){//判断是否是顶级部门
            path = "/"+deptId;
        }else {
            Long pid = productType.getParent().getId();
            path = super.getOne(pid).getPath() + "/" +deptId;
        }
        productType.setPath(path);
        super.modify(productType);
    }
}
